/**
 *    Copyright (C) 2009 Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

#ifndef _CUBEFS_CORE_API_REFLECTION_ARGUMENT_HPP
#define _CUBEFS_CORE_API_REFLECTION_ARGUMENT_HPP

#include <cubefs/api/reflection/operation.hpp>
#include <cubefs/core/api/reflection/type.hpp>

#define CUBEFS_SOURCE "cubefs/core/api/reflection/argument.hpp:" CUBEFS_AT

namespace cubefs { namespace api {

template <typename Interface>
class ArgumentImplT: public TypedImplT<Interface> {

	typedef ArgumentImplT<Interface>	Me;
	typedef TypedImplT<Interface>		Base;

    std::string name_;
    Type*       parameter_type_;
    bool        type_owner_;
public:
    ArgumentImplT(StringRef name, Type *type, bool type_owner = false):
    	name_(name), parameter_type_(type), type_owner_(type_owner) {}

    ArgumentImplT() {}

    virtual ~ArgumentImplT() throw () {
    	if (type_owner_) {
    		delete parameter_type_;
    	}
    }

    virtual StringRef name() const {
        return name_;
    }

    virtual Type* GetParameterType() const {
        return parameter_type_;
    }

    bool type_owner() const {
        return type_owner_;
    }

    static void InitType() {
    	type_ = new AbstractType(TypeNameFactory<Interface>::name(), Typed::MyType());
    }

    static void InitOperations() {
    	AbstractType *type = static_cast<AbstractType*>(type_);
    	type->AddOperation(new BasicOperation<StringRef (Me::*)(), StringRef (Me::*)() const>(type, "name", &Me::name));
		type->AddOperation(new BasicOperation<Typed* (Me::*)(), Type* (Me::*)() const>(type, Type::MyType(), "GetParameterType", &Me::GetParameterType));
    }

    CUBEFS_DECLARE_REFLECTION;
};


template <typename Interface>
Type* ArgumentImplT<Interface>::type_ 					= NULL;

typedef ArgumentImplT<Argument> 						ArgumentImpl;



}}

#undef CUBEFS_SOURCE
#endif
